<template>
  <van-address-edit :area-list="areaList" show-set-default :address-info="addressInfo" @save="onSave"
    @change-detail="onChangeDetail"></van-address-edit>
</template>

<script>
  import areaList from '../../static/area';
  export default {
    data() {
      return {
        areaList,
        addressInfo: {
          addressDetail: "",
          areaCode: "",
          city: "",
          county: "",
          isDefault: false,
          name: "",
          province: "",
          tel: ""
        }
      };
    },

    methods: {
      onSave(e) {
        let _this = this;

        let rowguid = _this.$route.query.rowguid
        let form = {};
        form.mailname = e.name;
        form.mailmobile = e.tel;
        form.provincename = e.province;
        form.cityname = e.city;
        form.countyname = e.county;
        form.address = e.addressDetail;
        form.countycode = e.areaCode;
        form.rowguid = rowguid;

        if (e.isDefault == true) {
          form.isdefault = '1';
        } else {
          form.isdefault = '0';
        }

        this.$api.addmailaddress(form).then(function (data, res) {
          _this.$router.push({
            path: '/addressmg',
            query: {
              rowguid: rowguid
            }
          })
        })
      },

      onChangeDetail(val) {

      }
    },
    created() {
      let _this = this;
      let rowguid = this.$route.query.rowguid;
      if (rowguid) {
        _this.$api.mailaddressdetail({
          rowguid: rowguid
        }).then(function (data) {

          _this.addressInfo.name = data.mailname;
          _this.addressInfo.tel = data.mailmobile;
          _this.addressInfo.province = data.provincename;
          _this.addressInfo.city = data.cityname;
          _this.addressInfo.county = data.countyname;
          _this.addressInfo.addressDetail = data.address;
          _this.addressInfo.areaCode = data.countycode;
          _this.addressInfo.rowguid = data.rowguid;
          if (data.isdefault == '1') {
            _this.addressInfo.isDefault = true;
          } else {
            _this.addressInfo.isDefault = false;
          }
          console.log(_this.addressInfo)
        })
      }
    }
  };
</script>